package com.sanxiau.dormitory.yang.controller;

import com.sanxiau.dormitory.liu.dao.ElectricDao;
import com.sanxiau.dormitory.liu.dao.NoticeDao;
import com.sanxiau.dormitory.liu.dao.StudentDao;
import com.sanxiau.dormitory.liu.entity.Electric;
import com.sanxiau.dormitory.liu.entity.Notice;
import com.sanxiau.dormitory.liu.entity.Student;
import com.sanxiau.dormitory.yang.entity.LoginIn;
import com.sanxiau.dormitory.yang.mapper.AttendanceMapper;
import com.sanxiau.dormitory.yang.service.LoginService;
import com.sanxiau.dormitory.yang.vo.ResultInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    LoginService loginService;
    @Autowired
    NoticeDao noticeDao;
    @Autowired
    AttendanceMapper attendanceMapper;
    @Autowired
    ElectricDao electricDao;
    @Autowired
    StudentDao studentDao;

    @ResponseBody
    @RequestMapping("/sendVerify")
    public ResultInfo sendVerify(String tel,HttpSession session){
        ResultInfo resultInfo = new ResultInfo();
        if (null==tel || "".equals(tel)){
            resultInfo.setMessage("请输入手机号");
        }else {
            resultInfo = loginService.SendVerify(tel, session);
        }
        //System.out.println(resultInfo);
        return resultInfo;
    }

    @RequestMapping("/userLogin1")
    public String userLogin1(Model model, String userName, String passWord,String res,HttpSession session){
        ResultInfo resultInfo = loginService.UserLogin1(userName, passWord,res);
        if (resultInfo.getMessage().equals("登陆成功")) {
            /*SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");*/
            SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            model.addAttribute("userName", resultInfo.getName());
            model.addAttribute("time", Timestamp.valueOf(simpleDate.format(resultInfo.getTime())));
            model.addAttribute("id", resultInfo.getId());
           /* System.out.println(Timestamp.valueOf(simpleDate.format(resultInfo.getTime())));*/

            //10条公告
            List<Notice> notices = new ArrayList<>();
            notices = noticeDao.findTen();
            model.addAttribute("notices",notices);

            //消息
            List<Electric> news1 = new ArrayList<>();
            Electric news = new Electric();
            //根据寝室号查询电费消息
            List<Student> students = studentDao.findByStuId(userName);
            if (!students.isEmpty() && null != students){
                String dorId = students.get(0).getDorId();
                news1 = electricDao.findByDorId(dorId);
            }else {
                news1 =null;
            }
            //如果没有需要缴费信息
            if (news1 == null || news1.isEmpty()){
                news.setId(1);
                news.setPayMonth("还未更新");
                news.setHouId("...");
                news.setDorId("...");
                news.setDegrees(0.00);
                news.setPrice(BigDecimal.valueOf(0.00));
                news.setAllPrice(BigDecimal.valueOf(0.00));
                news.setConductor("...");
                news.setPayState("...");
                model.addAttribute("news", news);
            }else {
                news = news1.get(0);
                model.addAttribute("news", news);
            }
        }else {
            model.addAttribute("error", resultInfo.getMessage());
            return "comm/Login";
        }
        LoginIn loginIn = attendanceMapper.queryUserByUser_name(resultInfo.getName());
        model.addAttribute("rank",loginIn.getRemark());
        session.setAttribute("userName",resultInfo.getName());
        return "comm/index";
    }

    @RequestMapping("/userLogin2")
    public String userLogin2(Model model,String link_tel,String verify,HttpSession session){
        ResultInfo resultInfo = new ResultInfo();
        String reg = "^1[3456789]\\d{9}$";
        if (null == link_tel){
            model.addAttribute("error1","请输入手机号");
            return "comm/Login";
        }else if (null == verify){
            model.addAttribute("error1","请输入验证码");
            return "comm/Login";
        }else {
            boolean rs = Pattern.matches(reg, link_tel);
            if (!rs) {
                model.addAttribute("error1", "请输入正确的手机号");
                return "comm/Login";
            } else {
                resultInfo = loginService.UserLogin2(link_tel, verify, session);
                if (resultInfo.getMessage().equals("登陆成功")) {
                    /*SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");*/
                    SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    model.addAttribute("userName", resultInfo.getName());
                    model.addAttribute("time", Timestamp.valueOf(simpleDate.format(resultInfo.getTime())));
                    model.addAttribute("id", resultInfo.getId());
                    /* System.out.println(Timestamp.valueOf(simpleDate.format(resultInfo.getTime())));*/
                    //10条公告
                    List<Notice> notices = new ArrayList<>();
                    notices = noticeDao.findTen();
                    model.addAttribute("notices", notices);

                    //消息
                    List<Electric> news1 = new ArrayList<>();
                    Electric news = new Electric();
                    //根据寝室号查询电费消息
                    List<Student> students = studentDao.findByStuId(resultInfo.getName());
                    if (!students.isEmpty() && null != students) {
                        String dorId = students.get(0).getDorId();
                        news1 = electricDao.findByDorId(dorId);
                    } else {
                        news1 = null;
                    }
                    //如果没有需要缴费信息
                    if (news1 == null || news1.isEmpty()) {
                        news.setId(1);
                        news.setPayMonth("还未更新");
                        news.setHouId("...");
                        news.setDorId("...");
                        news.setDegrees(0.00);
                        news.setPrice(BigDecimal.valueOf(0.00));
                        news.setAllPrice(BigDecimal.valueOf(0.00));
                        news.setConductor("...");
                        news.setPayState("...");
                        model.addAttribute("news", news);
                    } else {
                        news = news1.get(0);
                        model.addAttribute("news", news);
                    }
                } else {
                    model.addAttribute("error1", resultInfo.getMessage());
                    return "comm/Login";
                }
            }
        }
        LoginIn loginIn = attendanceMapper.queryUserByUser_name(resultInfo.getName());
        model.addAttribute("rank",loginIn.getRemark());
        session.setAttribute("userName",resultInfo.getName());
        return "comm/index";

    }
   /* @PostMapping("/userLogin3")
    public String userLogin3(Model model,String base64Data){

        ResultInfo resultInfo = null;
        try {
            resultInfo = loginService.UserLogin3(base64Data);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (resultInfo.getMessage().equals("登陆成功")) {
            SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            model.addAttribute("userName", resultInfo.getName());
            model.addAttribute("time", Timestamp.valueOf(simpleDate.format(resultInfo.getTime())));
            model.addAttribute("id", resultInfo.getId());

        }else {
            model.addAttribute("error", "登陆失败");
            return "yang/Login";
        }
        return "comm/index";

        //return resultInfo;
    }*/

    @RequestMapping("/quit")
    @ResponseBody
    public ResultInfo quit(String name,int id,HttpSession session){
        ResultInfo resultInfo = loginService.quit(name,id,session);
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return resultInfo;
    }
    @RequestMapping("/registerUser")
    public String registerUser(LoginIn loginIn,Model model){
        ResultInfo resultInfo = loginService.registerUser(loginIn);
        model.addAttribute("message",resultInfo.getMessage());
        return "yang/register";
    }
    @RequestMapping("/isExist")
    @ResponseBody
    public ResultInfo isExist(String user_name){
        ResultInfo resultInfo = loginService.isExist(user_name);
        return resultInfo;
    }

    /*@RequestMapping("/userlogin")
    public String userlogin(){
        return "yang/Login";
    }*/
/*    @RequestMapping("success")
    public String success(){
        return "dormitory/index";
    }*/
    @RequestMapping("register")
    public String resgister(){ return "yang/register";}
    @RequestMapping("Login")
    public String index(){ return "comm/Login";}



}
